/*
 * Copyright (C) 2016 The Fosafer Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fosafer.idcard_demo.util;

import com.fosafer.idcard_demo.app.IDCardApp;

/**
 * Create By :   IDEA
 * Package:      [com.fosafer.idcard_demo.util]
 * ClassName:    [DensityUtil]
 * Description:  [DensityUtil]
 * Author:       [jianzhic]
 * CreateDate:   [16-4-12 15:05]
 * UpdateUser:   [jianzhic]
 * UpdateDate:   [16-5-12 15:05]
 * UpdateRemark: [Update Description]
 * Version:      [v1.0.0]
 * <p>
 * Copyright (c) 2016 Fosafer Co.,Ltd. All rights reserved.
 */
public final class DensityUtil {

    private static float density = -1F;
    private static int widthPixels = -1;
    private static int heightPixels = -1;

    private DensityUtil() throws IllegalAccessException {
        throw new IllegalAccessException();
    }

    private static float getDensity() {
        if (density <= 0F) {
            density = IDCardApp.getInstance().getResources().getDisplayMetrics().density;
        }
        return density;
    }

    public static int dip2px(float dpValue) {
        return (int) (dpValue * getDensity() + 0.5F);
    }

    public static int px2dip(float pxValue) {
        return (int) (pxValue / getDensity() + 0.5F);
    }

    public static int getScreenWidth() {
        if (widthPixels <= 0) {
            widthPixels = IDCardApp.getInstance().getResources().getDisplayMetrics().widthPixels;
        }
        return widthPixels;
    }


    public static int getScreenHeight() {
        if (heightPixels <= 0) {
            heightPixels = IDCardApp.getInstance().getResources().getDisplayMetrics().heightPixels;
        }
        return heightPixels;
    }


}
